<?php
namespace backend\controllers;

use backend\models\AdminRoleModel;
use backend\models\OrganizationModel;
use Yii;
use yii\data\Pagination; //分页
use backend\base\BaseController;
use backend\models\AdminModel;
use yii\helpers\ArrayHelper;

/**
 * Admin controller
 */
class AdminController extends BaseController
{
    /*
     * 用户列表
     */
    public function actionList()
    {
        $data = parent::parseData($this->body);
        $admin = new AdminModel();
        $model = $admin->getList($data);
        $res = $model['data'];
        $role = AdminRoleModel::find()->select(['id','name'])->where(['id' => array_column($res,'role_id')])->asArray()->all();
        $role = ArrayHelper::map($role,'id','name');
        $create = AdminModel::find()->select(['id','name'])->where(['id' => array_column($res,'creator_id')])->asArray()->all();
        $create = ArrayHelper::map($create,'id','name');
        if(!empty($res)){
            foreach ($res as $k => $v) {
                //创建人
                $res[$k]['creator'] = @$create[$v['creator_id']];
                $res[$k]['role_name'] = @$role[$v['role_id']];
            }
        }
        $role_list = AdminRoleModel::getRoleList();
        $pagination=new Pagination([
            'pageSize' =>15,
            'totalCount'=>$model['count'],
        ]);
        return $this->render('list',array(
            'model' => $res,
            'pagination'=>$pagination,
            'data'=>$data,
            'role_list'=>$role_list
        ));
    }

    /*
     * @return 用户新增
     */
    public function actionAdd()
    {
        $model = new AdminModel(['scenario' => 'add']);
        $org = OrganizationModel::orgAll();
        if(Yii::$app->request->isPost){
            $data = parent::parseData($this->body);
            if ($model->load($data) && $model->validate() && $model->save()) {
                $this->addRecord('添加账户',$model->name);
                return $this->saveBack(['list']);
            }
        }
        $role_list = AdminRoleModel::getRoleList();
        return $this->render('add',array(
            'info' => $model,
            'role_list' => $role_list,
            'org'=>$org
        ));
    }

    /*
     * @return 用户编辑
     */
    public function actionEdit($id)
    {
        $data = parent::parseData($this->body);
        $model = AdminModel::findOne($id);
        $model->setScenario('edit');
        $org = OrganizationModel::orgAll();
        if(Yii::$app->request->isPost){
            if ($model->load($data) && $model->validate() && $model->save()) {
                $this->addRecord('编辑管理员',$model->name);
                return $this->saveBack(['list']);
            }
        }
        $role_list = AdminRoleModel::getRoleList();
        return $this->render('edit',array(
            'role_list' => $role_list,
            'info' => $model,
            'org'=>$org
        ));
    }

    /*
     * 修改状态,禁用|解禁
     */
    public function actionStatusAjaxDo()
    {
        $data = parent::parseData($this->body);
        $model = AdminModel::findOne($data['id']);
        if ($model && $model->id != 1) {
            $model->status = intval($data['status']);
            if($model->save()){
                $this->addRecord('禁用|解禁管理员',$model->name);
                return self::ReturnData('operate_success');
            }
        }
        return self::ReturnData('edit_error');
    }

    /*
     * 用户密码重置
     */
    public function actionResetPasswordAjaxDo()
    {
        $data = Yii::$app->request->post();
        $model = AdminModel::findOne((int)$data['id']);
        if ($model) {
            $model->password = Yii::$app->security->generatePasswordHash($data['password'].$model->create_time);
            if($model->save()){
                $this->addRecord('删除密码重置',$model->name);
                return self::ReturnData('pass_operate_success');
            }
        }
        return self::ReturnData('update_pass_error');
    }
}
